package com.jqr.activity.myschool


import android.os.Bundle

import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent

import androidx.compose.foundation.layout.fillMaxSize

import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface

import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState

import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview

import com.jqr.activity.myschool.https.ArticleRepository



import com.jqr.activity.myschool.ui.theme.MySchoolTheme
import dagger.hilt.android.AndroidEntryPoint

import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.jqr.activity.myschool.ui.pages.ArticleDetailPage
import com.jqr.activity.myschool.ui.pages.ArticleListPage
import com.jqr.activity.myschool.ui.pages.MainScreen
import com.jqr.activity.myschool.ui.pages.PageConstant
import com.jqr.activity.myschool.viewmodels.MainViewModel

@AndroidEntryPoint
class MainActivity: ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MySchoolTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {

                  //InitData()



                }
            }
        }
    }
}


@Composable
fun InitData(viewModel: MainViewModel= viewModel()){
    viewModel.changeRefresh(false)
    val  dataState=viewModel.result.observeAsState()
    dataState.value?.let { result->
        result.getOrNull()?.data?.data?.let {
            MainScreen(articles = it)
        }
    }
}


@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MySchoolTheme {
        InitData(viewModel = MainViewModel(ArticleRepository()))
    }
}